<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
	//添加分类
    public function getAdd()
    {
    	$data = self::Catelist();
    	//解析模板
    	return view('admin.cate.add',['cate'=>$data]);
    }

    //执行添加
    public function postInsert(Request $request)
    {
    	//判断数据
    	 $this->Validate($request, [
            'name' => 'required|max:20',
        ],[
        	'name.required'=>'分类名不能为空',
        	'name.max'=>'分类名不能超过20个字符'
        ]);
    		//接收数据
    	 $data = $request->only(['name','pid']);
    	 //判断是否为顶级分类
    	 if($data['pid'] == 0){
    	 	$data['path'] = '0';
    	 }else{
    	 	$res = DB::table('cate')->where('id',$data['pid'])->first();
    	 	$data['path'] = $res->path.','.$data['pid'];
    	 }
    	 $data['status'] = 1;
    	 
    	 //插入数据库
    	 $res = DB::table('cate')->insert($data);
    	 //判断
    	 if($res){
    	 	return redirect('/admin/cate/index')->with('success','分类添加成功');
    	 }else{
    	 	return back()->with('error','分类添加失败');
    	 }
    }

    //分类列表
    public function getIndex(Request $request)
    {
        //搜索
        $num = $request->input('num',10);
        if($request->input('cname')){
            $cate = DB::table('cate')
            ->where('name','like','%'.$request->input('cname').'%')
            ->paginate($num);
        }else{
            $cate = DB::table('cate')
            ->select(DB::raw('*,concat(path,",",id) as paths'))
            ->orderBy('paths')
            ->paginate($num);
        }
        //遍历 加|----
        foreach($cate as $k=>$v){
            // 转化成数组
            $arr = explode(',',$v->path);
            //统计长度
            $len = count($arr)-1;
            //替换
            $v->name = str_repeat('|----',$len).$v->name;
        }
        //遍历该父级名字
        foreach($cate as $k=>$v){
            if($cate[$k]->pid == 0){
               $cate[$k]->path = '顶级分类';
            }else{
                $res = DB::table('cate')->where('id',$cate[$k]->pid)->first();
                $cate[$k]->path = $res->name;
            }
        }
        $data = $request->all();
    	return view('admin.cate.index',['cate'=>$cate,'request'=>$data]);
    }

    //排序查询 静态方法
    static function Catelist()
    {
    	//查询
    	$data = DB::select('select *,concat(path,",",id) as paths from cate order by paths');

    	//遍历  加|----
    	foreach($data as $k=>$v)
    	{
    		//转成数组
    		$arr = explode(',',$v->path);
    		//统计长度
    		$len = count($arr)-1;
    		//重复字符串
    		$v->name = str_repeat('|----',$len).$v->name;
    	}	

    	return $data;
    }

    //ajax删除
    public function postDelete(Request $request)
    {
        //接收数据
        $id = $request->input('id');
        //查询是否有子类
        $num = DB::table('cate')->where('pid',$id)->count();
        //判断
        if($num > 0 ){
            return 2;
        }
        //删除
        $res = DB::table('cate')->where('id',$id)->delete();
        //判断
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

    //修改
    public function getEdit($id)
    {
        //查询这条数据
        $data = DB::table('cate')->where('id',$id)->first();
        //查询所有分类
        $cate = self::Catelist();
        //解析模板
        return view('admin.cate.edit',['data'=>$data,'cate'=>$cate]);
    }

    //执行修改
    public function postUpdate(Request $request)
    {
        //接收数据
        $id = $request->input('id');
        //检测是否要修改 如果修改 id 和 pid 就不相等
        if($request->input('id') != $request->input('pid')){
            //检测是否有子类
            $res = DB::table('cate')->where('pid',$id)->count();
            //判断
            if($res > 0){
                return back()->with('error','该类下有子类,不能修改');
            }
        }
        //修改路径
        $data = $request->only(['name','pid']);
        if($data['pid'] == 0){
            $data['path'] = 0;
        }else{
            $res = DB::table('cate')->where('id',$data['pid'])->first();
            $data['path'] = $res->path.','.$data['pid'];
        }

        //修改
        $res = DB::table('cate')->where('id',$id)->update($data);
        //判断
        if($res){
            return redirect('/admin/cate/index')->with('success','分类修改成功');
        }else{
            return back()->with('error','分类修改失败');
        }
    }

    //分类多维数组
    public static function CateAll($pid)
    {
        $res = DB::table('cate')->where('pid',$pid)->get();
        $data = [];
        foreach($res as $k=>$v){
            $v->stu = self::CateAll($v->id);
            $data[] = $v;
        }
        return $data;
    }
}
